package com.controller;

import com.bean.BootStrapTableExpress;
import com.bean.Express;
import com.bean.Message;
import com.bean.ResultData;
import com.mvc.ResponseBody;
import com.service.ExpressService;
import com.util.DateFormatUtil;
import com.util.JSONUtil;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.DataFormatException;

public class ExpressController {
    ExpressService expressService = new ExpressService();

    @ResponseBody("/expresss/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = expressService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Express> expressList = expressService.findAll(true, offset, pageNumber);
        List<BootStrapTableExpress> newList = new ArrayList<>();
        for (Express express : expressList) {
            String inTime = DateFormatUtil.format(express.getInTime());
            String outTime = express.getOutTime() == null ? "未出库" : DateFormatUtil.format(express.getOutTime());
            String status = express.getStatus() == 0 ? "待取件" : "已取件";
            String code = express.getCode() == null ? "已取件" : express.getCode();
            BootStrapTableExpress bootStrapTableExpress = new BootStrapTableExpress(express.getId(), express.getNumber(), express.getUserName(), express.getUserPhone(), express.getCompany(), code, inTime, outTime, status, express.getSysPhone());
            newList.add(bootStrapTableExpress);
        }
        List<Map<String, Integer>> console = expressService.console();
        Integer total = console.get(0).get("data1_size");
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(newList);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        Express express = new Express(number, userName, userPhone, company, UserUtil.getUserPhone(request.getSession()), null);
        boolean flag = expressService.insert(express);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("快递录入成功!");
        } else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        Express express = expressService.findByNumber(number);
        //System.out.println(express.getUserName());
        Message msg = new Message();
        if (express == null) {
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(express);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUserName(userName);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);
        boolean flag = expressService.update(id, newExpress);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = expressService.delete(id);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
}
